import data_control from "../../engines/libs/data_control";

const {ccclass, property, menu , disallowMultiple} = cc._decorator;

@ccclass
@menu('工具组件/质点')
export default class Mass extends cc.Component{
    // private target : cc.Node = null;
    private iMass : number = 1;
    
    public position : cc.Vec2 = new cc.Vec2();
    public velocity : cc.Vec2 = new cc.Vec2();

    private allForce : cc.Vec2[] = [];
    private airRatio:number = 0;

    public setTime : boolean = false;

    private force : cc.Vec2 = null;

    private onTouched : ()=>void = null;

    onLoad(){
        this.position = this.node.getPosition();
    }

    update(dt){
        // if(this.setTime){
        //     this.AddForce(this.position.sub(this.target.getPosition()).normalize().scale(this.force))
        // }
        
        let totalForce = data_control.AllArraySum(this.allForce);
        let airFri : cc.Vec2 = this.velocity.normalize().mul(-1);
        let airFriRatio : number = this.velocity.mag()*Math.pow(this.airRatio,2);
        airFri.scaleSelf(cc.v2(airFriRatio,airFriRatio));
    
        totalForce.addSelf(airFri);
        this.velocity = this.velocity.add(totalForce.scale(cc.v2(this.iMass,this.iMass)).mul(dt));
        // this.node.angle = -data_control.GetAngle(this.velocity);
        this.position.addSelf(this.velocity.mul(dt));
        // console.warn(this.velocity.x,this.velocity.y);
        this.node.setPosition(this.position);
    }

    lateUpdate(dt){
        // if(this.target.getPosition().sub(this.position).mag()<=50){
        //     this.onTouched && this.onTouched();
        //     this.node.destroy();
        // }
        this.allForce = [];
    }

    AddForce(force:cc.Vec2){
        this.allForce.push(force);
    }

    SetVelocity(value:cc.Vec2){
        this.velocity = value;
    }
    SetPosition(value:cc.Vec2){
        this.position = value;
        this.node.setPosition(value);
    }
}